// pages/add-book/add-book.js
const requestToAddHouse = require('../../utils/request.js').requestToAddHouse
const requestToGetHouse = require('../../utils/request.js').requestToGetHouse
const requestToAddBook = require('../../utils/request.js').requestToAddBook
const successBackPage = require('../../utils/wx-navigate.js').successBackPage


//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseId:'',
    houseName:'',
    nameInput: { label: "租客姓名", placeholder: "请输入租客姓名", maxlength:10,value:''},
    phoneInput: { label: "手机号码", placeholder: "请输入租客手机号", maxlength: 11, type:"number",value: '' },

    rentBeginDateSelect: { label: "起租日期", placeholder: "请选择起租日期", mode: "date", value: '' },
    rentMonthCountSelect: { label: "租期时长", placeholder: "请选择租期时长", mode: "selector",
      range: ['1个月', '2个月', '3个月', '4个月', '5个月', '6个月', '12个月', '24个月'],index:'' },

    payMonthCountInput: { label: "交租方式", value: '1月/付', disabled:true },
    payInDaySelect: {
      label: "交租日期", placeholder: "请选择交租日期", mode: "selector",
      range: ['1号', '2号', '3号', '4号', '5号', '6号', '7号', '8号', '9号', '10号', '11号', '12号', '13号', '14号', '15号', '16号', '17号', '18号', '19号', '20号', '21号', '22号', '23号', '24号', '25号', '26号', '27号', '28号', '29号', '30号', '31号'], index: ''
    },

    moneyPerMonthInput: { label: "房屋押金", placeholder: "请输入押金金额", maxlength: 9, type: "digit",value: '' },
    moneySecureInput: { label: "每月租金", placeholder: "请输入每月租金金额", maxlength: 9, type: "digit",value: '' },
  },

  bindinput: function (e) {
    let input
    switch (e.currentTarget.dataset.id){
      // 租客姓名
      case this.data.nameInput.label:
        input = this.data.nameInput
        input.value = e.detail.value
        this.setData({
          nameInput: input
        })
        break
      // 手机号码
      case this.data.phoneInput.label:
        input = this.data.phoneInput
        input.value = e.detail.value
        this.setData({
          phoneInput: input
        })
        break
      // 房租押金
      case this.data.moneyPerMonthInput.label:


        input = this.data.moneyPerMonthInput
        let oldInputValue = input.value
        let newInputValue = e.detail.value
        if (newInputValue.indexOf('.') !== newInputValue.lastIndexOf('.')) {
          input.value = oldInputValue
        } else {
          if (newInputValue.indexOf('.') >= 0 && newInputValue.split('.')[1].length > 2) {
            input.value = oldInputValue
          } else {
            input.value = newInputValue
          }
        }
        this.setData({
          moneyPerMonthInput: input
        })
        console.log(input)
        break
      // 每月租金
      case this.data.moneySecureInput.label:

        input = this.data.moneySecureInput
        let oldInputValue1 = input.value
        let newInputValue1 = e.detail.value
        if (newInputValue1.indexOf('.') !== newInputValue1.lastIndexOf('.')) {
          input.value = oldInputValue1
        } else {
          if (newInputValue1.indexOf('.') >= 0 && newInputValue1.split('.')[1].length > 2) {
            input.value = oldInputValue1
          } else {
            input.value = newInputValue1
          }
        }
        this.setData({
          moneySecureInput: input
        })

        break
    }
    
  },

  bindchange: function (e) {
    let select
    switch (e.currentTarget.dataset.id) {
      // 起租日期
      case this.data.rentBeginDateSelect.label:
        select = this.data.rentBeginDateSelect
        select.value = e.detail.value
        this.setData({
          rentBeginDateSelect: select
        })
        console.log("起租日期")
        console.log(this.data.rentBeginDateSelect)
        break
      // 租期时长
      case this.data.rentMonthCountSelect.label:
        select = this.data.rentMonthCountSelect
        select.index = e.detail.value
        this.setData({
          rentMonthCountSelect: select
        })

        console.log("租期时长")
        console.log(this.data.rentMonthCountSelect)
        break

      // 交租日期
      case this.data.payInDaySelect.label:
        select = this.data.payInDaySelect
        select.index = e.detail.value
        this.setData({
          payInDaySelect: select
        })
        console.log("租期时长")
        console.log(this.data.payInDaySelect)
        break
    }
  },

  toAddBook:function(){

    if (this.data.phoneInput.value.length!==11){
      wx.showToast({
        title: "手机号格式不正确",
        icon: 'none',
        mask: true,
      })

      return
    }

    let book = {
      house_id: this.data.houseId,
      renter_phone: this.data.phoneInput.value,
      renter_name: this.data.nameInput.value,
      rent_begin_date: this.data.rentBeginDateSelect.value,
      rent_month_count: this.data.rentMonthCountSelect.range[this.data.rentMonthCountSelect.index],
      pay_in_day: this.data.payInDaySelect.range[this.data.payInDaySelect.index],
      pay_month_count: this.data.payMonthCountInput.value,
      money_secure: parseInt(this.data.moneyPerMonthInput.value * 100),
      money_per_month: parseInt(this.data.moneySecureInput.value * 100),
    }

    


    

    requestToAddBook(book,res=>{

      app.updateIndexPageData()
      successBackPage('保存成功')
    })
    // requestToAddHouse(this.data.region, this.data.houseName)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(options.data)
    this.setData({
      houseId: data._id,
      houseName: data.house_name
    })
    console.log(data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})